import StorageManager from '@/utils/StorageManager';
import type { FC } from 'react';
import { Prompt } from 'umi';

/**
 * 页面跳转提示
 * - message: 提示语
 * - when: 是否跳转前提示
 * - onOk: 确定回调
 * - onCancel: 取消回调
 */
type TProps = {
  message?: string;
  when?: boolean;
  onOk?: TAnyFunction;
  onCancel?: TAnyFunction;
};

const CommonPrompt: FC<TProps> = (props) => {
  const { message = '您确定要离开该页面吗', when = true, onOk, onCancel } = props;

  return (
    <Prompt
      message={() => {
        const token = StorageManager.getCookie('token');
        if (!token) {
          return true;
        }
        const leave = window.confirm(message);
        if (!leave) {
          onCancel && onCancel();
          return false;
        }
        onOk && onOk();
        return true;
      }}
      when={when}
    />
  );
};

export default CommonPrompt;
